package com.quickstart.wx.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author:wesley
 */
@Controller
@RequestMapping(value = "/config")
public class ConfigServerController {

    @Autowired
    private WxCommonUtils wxCommonUtils;


    /**
     * 这是腾讯的微信服务验证服务号的方法
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @param response
     */
    @RequestMapping(value = "/server")
    public void testServerConfig(String signature, String timestamp, String nonce, String echostr, HttpServletResponse response){
        System.out.println("************wxServer test **********");
        System.out.println("signature : "+signature);
        System.out.println("timestamp : "+timestamp);
        System.out.println("nonce : "+nonce);
        System.out.println("echostr : "+echostr);
        System.out.println("**********************");

        try {
            response.getWriter().print(echostr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * 微信的js-sdk的参数
     *
     * @param url 页面请求的url ， 扣除#以后
     * @return
     */
    @RequestMapping(value = "/wx/config")
    @ResponseBody
    public Map<String,String> getWxConfig(String url){
        String ticket = wxCommonUtils.getJsTicketOrAccessToken(2);
        Map<String, String> map = wxCommonUtils.sign(ticket, url);
        return map;
    }




}
